package com.loop.unclelend.utils.helper

import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/8/14 12:04
 * @Annotation：自定义数据 帮助类
 */
object CustomConfigsHelper {

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "homeAmount": "22000",
     *         "maxAnnualRate": "26%"
     *     },
     */

    /**
     * 获取客服配置
     */
    fun getUncleConfigData(): JSONObject? {
        val dataString = SharedHelper.getUncleSPString(SharedHelper.UNCLE_SPKEY.UNCLE_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun getUncleCustomData(): JSONObject?{
        val customize = getUncleConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取权限列表
     */
    fun getUnclePermissionsList(): ArrayList<String>{
        val permissionList = arrayListOf<String>()
        val customize = getUncleConfigData()
        if(customize != null){
            val permissions = customize.optJSONObject("permissions")
            if (permissions != null){
                val permission = permissions.optJSONArray("permission")
                if (permission != null && permission.length() > 0){
                    for (i in 0..<permission.length()){
                        permissionList.add(permission.optString(i))
                    }
                }

                val home = permissions.optJSONArray("home")
                if (home != null && home.length() > 0){
                    for (i in 0..<home.length()){
                        val str = home.optString(i)
                        if (permissionList.contains(str)){
                            continue
                        }
                        permissionList.add(str)
                    }
                }
            }
        }
        return permissionList
    }
}